package com.lineage.server.model.Instance;

import com.lineage.config.ConfigOther;
import com.lineage.server.IdFactoryNpc;
import com.lineage.server.datatables.SkillsTable;
import com.lineage.server.model.L1CastleLocation;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1Magic;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.doll.Doll_Skill;
import com.lineage.server.model.doll.L1DollExecutor;
import com.lineage.server.model.skill.L1SkillUse;
import com.lineage.server.serverpackets.S_DoActionGFX;
import com.lineage.server.serverpackets.S_MoveCharPacket;
import com.lineage.server.serverpackets.S_NPCPack_Doll;
import com.lineage.server.serverpackets.S_PacketBox;
import com.lineage.server.serverpackets.S_SkillBrave;
import com.lineage.server.serverpackets.S_SkillHaste;
import com.lineage.server.serverpackets.S_SkillSound;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Doll;
import com.lineage.server.templates.L1Npc;
import com.lineage.server.templates.L1Skills;
import com.lineage.server.templates.L1SupperDoll;
import com.lineage.server.timecontroller.server.ServerWarExecutor;
import com.lineage.server.types.Point;
import com.lineage.server.world.World;
import com.lineage.server.world.WorldItem;

import static com.lineage.server.model.skill.L1SkillId.GREATER_HASTE;
import static com.lineage.server.model.skill.L1SkillId.HASTE;
import static com.lineage.server.model.skill.L1SkillId.STATUS_HASTE;

import java.util.Iterator;
import java.util.Random;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1DollInstance extends L1NpcInstance {
	
  private Boolean _isSupperDoll = false;	
	
  private static final long serialVersionUID = 1L;
  
  private static final Log _log = LogFactory.getLog(L1DollInstance.class);
  
  private static Random _random = new Random();
  
  private int _itemObjId;
  
  private boolean _power_doll;
  
  private L1Doll _type;
  
  private L1SupperDoll _type1;
  
  private int _time;
  
  private int _skillid;
  
  private int[] _skill_id;
  
  private int[] _skill_mp;
  
  private int _r;
  
  private int _olX;
  
  private int _olY;
  
  private L1ItemInstance _item;
  
  
	
  public Boolean get_isSupperDoll() {
	return _isSupperDoll;
}


public void set_isSupperDoll(Boolean _isSupperDoll) {
	this._isSupperDoll = _isSupperDoll;
}
  
  public L1DollInstance(L1Npc template, L1PcInstance master, int itemObjId, L1Doll type, boolean power_doll) {
    super(template);
    this._power_doll = false;
    this._time = 0;
    this._skillid = -1;
    this._r = -1;
    this._olX = 0;
    this._olY = 0;
    this._item = null;
    try {
      this._power_doll = power_doll;
      setId(IdFactoryNpc.get().nextId());
      set_showId(master.get_showId());
      setItemObjId(itemObjId);
      this._type = type;
      setGfxId(type.get_gfxid());
      setTempCharGfx(type.get_gfxid());
      setNameId(type.get_nameid());
      set_time(type.get_time());
      setMaster(master);
      setX(master.getX() + _random.nextInt(5) - 2);
      setY(master.getY() + _random.nextInt(5) - 2);
      setMap(master.getMapId());
      setHeading(5);
      setLightSize(template.getLightSize());
      World.get().storeObject((L1Object)this);
      World.get().addVisibleObject((L1Object)this);
      master.setUsingDoll(this);
      master.addDoll(this);
      Iterator<L1PcInstance> iterator = World.get().getRecognizePlayer((L1Object)this).iterator();
      while (iterator.hasNext()) {
        L1PcInstance pc = iterator.next();
        onPerceive(pc);
      } 
      if (this._power_doll) {
        master.add_power_doll(this);
      } else {
        master.addDoll(this);
      } 
      L1PcInstance masterpc = null;
      if (this._master instanceof L1PcInstance) {
        masterpc = (L1PcInstance)this._master;
        if (!this._type.getPowerList().isEmpty()) {
          Iterator<L1DollExecutor> iterator2 = this._type.getPowerList().iterator();
          while (iterator2.hasNext()) {
            L1DollExecutor power = iterator2.next();
            if (power instanceof Doll_Skill) {
              Doll_Skill skill = (Doll_Skill)power;
              set_skill(skill.get_int()[0], skill.get_int()[1]);
              continue;
            } 
            power.setDoll(masterpc);
          } 
        } 
        master.sendPackets((ServerBasePacket)new S_PacketBox(56, type.get_time()));
      } 
      broadcastPacketAll((ServerBasePacket)new S_SkillSound(getId(), 5935));
      set_olX(getX());
      set_olY(getY());
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  
  public L1DollInstance(L1Npc template, L1PcInstance master, int itemObjId, L1SupperDoll type, boolean power_doll) {
	    super(template);
	    this._power_doll = false;
	    this._isSupperDoll = true;
	    this._time = 0;
	    this._skillid = -1;
	    this._r = -1;
	    this._olX = 0;
	    this._olY = 0;
	    this._item = null;
	    try {
	      this._power_doll = power_doll;
	      setId(IdFactoryNpc.get().nextId());
	      set_showId(master.get_showId());
	      setItemObjId(itemObjId);
	      this._type1 = type;
	      setGfxId(type.get_gfxid());
	      setTempCharGfx(type.get_gfxid());
	      setNameId(type.get_nameid());
	      set_time(type.get_time());
	      setMaster(master);
	      setX(master.getX() + _random.nextInt(5) - 2);
	      setY(master.getY() + _random.nextInt(5) - 2);
	      setMap(master.getMapId());
	      setHeading(5);
	      setLightSize(template.getLightSize());
	      World.get().storeObject((L1Object)this);
	      World.get().addVisibleObject((L1Object)this);
	      master.setUsingDoll(this);
	      master.addDoll(this);
	      Iterator<L1PcInstance> iterator = World.get().getRecognizePlayer((L1Object)this).iterator();
	      while (iterator.hasNext()) {
	        L1PcInstance pc = iterator.next();
	        onPerceive(pc);
	      } 
	      if (this._power_doll) {
	        master.add_power_doll(this);
	      } else {
	        master.addDoll(this);
	      } 
	      L1PcInstance masterpc = null;
	      if (this._master instanceof L1PcInstance) {
	        masterpc = (L1PcInstance)this._master;
	        if (!this._type1.getPowerList().isEmpty()) {
	          Iterator<L1DollExecutor> iterator2 = this._type1.getPowerList().iterator();
	          while (iterator2.hasNext()) {
	            L1DollExecutor power = iterator2.next();
	            if (power instanceof Doll_Skill) {
	              Doll_Skill skill = (Doll_Skill)power;
	              set_skill(skill.get_int()[0], skill.get_int()[1]);
	              continue;
	            } 
	            power.setDoll(masterpc);
	          } 
	        } 
	        master.sendPackets((ServerBasePacket)new S_PacketBox(56, type.get_time()));
	      } 
	      broadcastPacketAll((ServerBasePacket)new S_SkillSound(getId(), 5935));
	      set_olX(getX());
	      set_olY(getY());
	    } catch (Exception e) {
	      _log.error(e.getLocalizedMessage(), e);
	    } 
	    set_isSupperDoll(true);
	  }
  
  
  public L1DollInstance(L1Npc template, L1NpcInstance master, L1Doll type) {
    super(template);
    this._power_doll = false;
    this._time = 0;
    this._skillid = -1;
    this._r = -1;
    this._olX = 0;
    this._olY = 0;
    this._item = null;
    try {
      setId(IdFactoryNpc.get().nextId());
      set_showId(master.get_showId());
      this._type = type;
      setGfxId(type.get_gfxid());
      setTempCharGfx(type.get_gfxid());
      setNameId(type.get_nameid());
      set_time(type.get_time());
      set_time(1800);
      setMaster(master);
      setX(master.getX() + _random.nextInt(5) - 2);
      setY(master.getY() + _random.nextInt(5) - 2);
      setMap(master.getMapId());
      setHeading(5);
      setLightSize(template.getLightSize());
      World.get().storeObject((L1Object)this);
      World.get().addVisibleObject((L1Object)this);
      Iterator<L1PcInstance> iterator = World.get().getRecognizePlayer((L1Object)this).iterator();
      while (iterator.hasNext()) {
        L1PcInstance pc = iterator.next();
        onPerceive(pc);
      } 
      master.addDoll(this);
      broadcastPacketX10((ServerBasePacket)new S_SkillSound(getId(), 5935));
      set_olX(getX());
      set_olY(getY());
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  
  public L1DollInstance(L1Npc template, L1NpcInstance master, L1SupperDoll type) {
	    super(template);
	    this._power_doll = false;
	    this._isSupperDoll = true;
	    this._time = 0;
	    this._skillid = -1;
	    this._r = -1;
	    this._olX = 0;
	    this._olY = 0;
	    this._item = null;
	    try {
	      setId(IdFactoryNpc.get().nextId());
	      set_showId(master.get_showId());
	      this._type1 = type;
	      setGfxId(type.get_gfxid());
	      setTempCharGfx(type.get_gfxid());
	      setNameId(type.get_nameid());
	      set_time(type.get_time());
	      set_time(1800);
	      setMaster(master);
	      setX(master.getX() + _random.nextInt(5) - 2);
	      setY(master.getY() + _random.nextInt(5) - 2);
	      setMap(master.getMapId());
	      setHeading(5);
	      setLightSize(template.getLightSize());
	      World.get().storeObject((L1Object)this);
	      World.get().addVisibleObject((L1Object)this);
	      Iterator<L1PcInstance> iterator = World.get().getRecognizePlayer((L1Object)this).iterator();
	      while (iterator.hasNext()) {
	        L1PcInstance pc = iterator.next();
	        onPerceive(pc);
	      } 
	      master.addDoll(this);
	      broadcastPacketX10((ServerBasePacket)new S_SkillSound(getId(), 5935));
	      set_olX(getX());
	      set_olY(getY());
	      set_isSupperDoll(true);
	    } catch (Exception e) {
	      _log.error(e.getLocalizedMessage(), e);
	    } 
	  }
	  
  
  
  public void deleteDoll() {
    try {
      L1ItemInstance item = WorldItem.get().getItem(Integer.valueOf(this._itemObjId));
      if (item != null)
        item.stopEquipmentTimer((L1PcInstance)this._master); 
      broadcastPacketAll((ServerBasePacket)new S_SkillSound(getId(), 5936));
      L1PcInstance masterpc = null;
      if (this._master instanceof L1PcInstance) {
        masterpc = (L1PcInstance)this._master;
        
        
        Boolean a = this.get_isSupperDoll();
        if (a) {
            if (!this._type1.getPowerList().isEmpty() ) {
                Iterator<L1DollExecutor> iterator = this._type1.getPowerList().iterator();
                while (iterator.hasNext()) {
                  L1DollExecutor p = iterator.next();
                  p.removeDoll(masterpc);
                } 
              } 
		}else {
        if (!this._type.getPowerList().isEmpty() ) {
          Iterator<L1DollExecutor> iterator = this._type.getPowerList().iterator();
          while (iterator.hasNext()) {
            L1DollExecutor p = iterator.next();
            p.removeDoll(masterpc);
          } 
        } 
		}
        masterpc.sendPackets((ServerBasePacket)new S_PacketBox(56, 0));
      } 
      if (this._power_doll) {
        this._master.remove_power_doll();
        this._master.removeDoll(this);
      } else {
        this._master.removeDoll(this);
      } 
      deleteMe();
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void onPerceive(L1PcInstance perceivedFrom) {
    try {
      if (perceivedFrom.get_showId() != get_showId())
        return; 
      perceivedFrom.addKnownObject((L1Object)this);
      perceivedFrom.sendPackets((ServerBasePacket)new S_NPCPack_Doll(this, perceivedFrom));
      if (getBraveSpeed() > 0)
        perceivedFrom.sendPackets((ServerBasePacket)new S_SkillBrave(getId(), getBraveSpeed(), 600000)); 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void setNpcMoveSpeed() {
    try {
      if (!ConfigOther.WAR_DOLL) {
        int castle_id = 1;
        while (castle_id < 8) {
          if (ServerWarExecutor.get().isNowWar(castle_id) && 
            L1CastleLocation.checkInWarArea(castle_id, this)) {
            deleteDoll();
            return;
          } 
          castle_id++;
        } 
      } 
      if (this._master != null && this._master.isInvisble()) {
        deleteDoll();
        return;
      } 
      if (this._master.isDead()) {
        deleteDoll();
        return;
      } 
      if (this._master.getMoveSpeed() != getMoveSpeed())
        setMoveSpeed(this._master.getMoveSpeed()); 
      if (this._master.getBraveSpeed() != getBraveSpeed())
        setBraveSpeed(this._master.getBraveSpeed()); 
      if (this._master != null && this._master.getMapId() == getMapId()) {
        if (getLocation().getTileLineDistance((Point)this._master.getLocation()) > 2) {
          int dir = targetDirection(this._master.getX(), this._master.getY());
          Iterator<L1Object> iterator = World.get().getVisibleObjects((L1Object)this, 1).iterator();
          while (iterator.hasNext()) {
            L1Object object = iterator.next();
            if (dir >= 0 && dir <= 7) {
              int locx = getX() + L1Character.HEADING_TABLE_X[dir];
              int locy = getY() + L1Character.HEADING_TABLE_Y[dir];
              if (!(object instanceof L1DollInstance) || locx != object.getX() || locy != object.getY())
                continue; 
              dir++;
            } 
          } 
          if (dir >= 0 && dir <= 7) {
            setDirectionMoveSrc(dir);
            broadcastPacketAll((ServerBasePacket)new S_MoveCharPacket(this));
          } 
        } 
      } else {
        deleteDoll();
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public int getItemObjId() {
    return this._itemObjId;
  }
  
  public void setItemObjId(int i) {
    this._itemObjId = i;
  }
  
  public int get_time() {
    return this._time;
  }
  
  public void set_time(int time) {
    this._time = time;
  }
  
  
//  public void startDollAddSkill() {
//	try {  
//
//		
//		for (L1DollExecutor e : this._type.getPowerList()) {
//			e.get
//		}
//		
//		
//		if (_skillid != -1) {
//			L1Skills skill = SkillsTable.get().getTemplate(_skillid);
//			int castgfx = skill.getCastGfx();
//			switch (_skillid) {
//			case 26:
//			case 42:
//			case 149:
//			case 48:
//			case 151:
//			case 148:
//			case 168:
//			case 163:
//			case 166:
//			case 79:
//			case 68:
//				if (!_master.hasSkillEffect(_skillid)){
//			        L1SkillUse skillUse = new L1SkillUse();
//			        skillUse.handleCommands((L1PcInstance)_master, _skillid, _master.getId(), _master.getX(), _master.getY(), skill.getBuffDuration(), 4);
//				}
//				break;
//
//			default:
//				break;
//			}	
//		}
//	  
//	} catch (Exception e) {
//		_log.error(e.getLocalizedMessage(), e);
//	}
//  }
//  
  
	/**
	 * 執行娃娃施放技能
	 * 
	 * @param target
	 *            被攻擊者
	 * @param calculated
	 *            是否已預先計算過機率
	 */
	public void startDollSkill(L1Character target, boolean calculated) {
		try {
			boolean start = false;// 施展技能
			if (_skillid != -1) {
				L1Skills skill = SkillsTable.get().getTemplate(_skillid);
				int castgfx = skill.getCastGfx();

				if (!calculated && _random.nextInt(100) <= _r) {
					start = true;
				} else if (calculated) {
					start = true;
				}

				if (start) {// 施展技能
					switch (_skillid) {
					case 4:// 光箭 1100
					case 6:// 冰箭 1100
					case 7:// 風刃 1100
					case 10:// 寒冷戰慄 1100
					case 15:// 火箭 1300
					case 16:// 地獄之牙 1300
					case 17:// 極光雷電 1500
					case 18:// 起死回生術 1500
					case 22:// 寒冰氣息 1300
					case 25:// 燃燒的火球 1600
					case 28:// 吸血鬼之吻 1250
					case 30:// 巖牢 1600
					case 34:// 極道落雷 1250
					case 38:// 冰錐 1100
					case 45:// 地裂術 1100
					case 46:// 烈炎術 1650
					case 50:// 冰矛圍籬 1600
					case 58:// 火牢 1100
					case 65:// 雷霆風暴 3000
					case 74:// 流星雨 4000
					case 77:// 究極光裂術 6000
					case 189:// 岩漿之箭
					case 192:// 奪命之雷 5150
					case 203:// 粉碎能量 3100
					case 207:// 心靈破壞 4050
						// broadcastPacketAll(new S_DoActionGFX(getId(), 67));
						L1Magic magic = new L1Magic(_master, target);
						int magic_dmg = magic.calcMagicDamage(_skillid);
						magic.commit(magic_dmg, 0);

						target.broadcastPacketAll(new S_SkillSound(target.getId(), castgfx));
						if (target instanceof L1PcInstance) {
							L1PcInstance pc = (L1PcInstance) target;
							pc.sendPackets(new S_SkillSound(pc.getId(), castgfx));
						}
						break;
					case 29:// 緩速術 1600
						switch (target.getMoveSpeed()) {
						case 0:
							if (target instanceof L1PcInstance) {
								final L1PcInstance pc = (L1PcInstance) target;
								pc.sendPackets(new S_SkillHaste(pc.getId(), 2, skill.getBuffDuration()));
							}
							target.broadcastPacketAll(new S_SkillHaste(target.getId(), 2, skill.getBuffDuration()));
							target.setMoveSpeed(2);
							break;

						case 1:
							int skillNum = 0;
							if (target.hasSkillEffect(HASTE)) {
								skillNum = HASTE;

							} else if (target.hasSkillEffect(GREATER_HASTE)) {
								skillNum = GREATER_HASTE;

							} else if (target.hasSkillEffect(STATUS_HASTE)) {
								skillNum = STATUS_HASTE;
							}

							if (skillNum != 0) {
								target.removeSkillEffect(skillNum);
								target.setMoveSpeed(0);
							}
							break;
						}
						break;
					case 11:// 毒咒 1650
						L1SkillUse skillUse = new L1SkillUse();
						skillUse.handleCommands(null, _skillid, target.getId(), target.getX(), target.getX(), 0,
								L1SkillUse.TYPE_GMBUFF, this);
						break;
					case 5000:// 火焰衝擊
						if (target instanceof L1PcInstance) {
							L1PcInstance pc = (L1PcInstance) target;
							pc.sendPacketsAll(new S_SkillSound(pc.getId(), castgfx));
							pc.receiveDamage(_master, 80, true, true);
						} else if (target instanceof L1NpcInstance) {
							L1NpcInstance npc = (L1NpcInstance) target;
							npc.broadcastPacketAll(new S_SkillSound(npc.getId(), castgfx));
							npc.receiveDamage(_master, 80);
						}
						break;
					case 5001:// 爆擊火焰衝擊
						if (target instanceof L1PcInstance) {
							L1PcInstance pc = (L1PcInstance) target;
							pc.sendPacketsAll(new S_SkillSound(pc.getId(), castgfx));
							pc.receiveDamage(_master, 160, true, true);
						} else if (target instanceof L1NpcInstance) {
							L1NpcInstance npc = (L1NpcInstance) target;
							npc.broadcastPacketAll(new S_SkillSound(npc.getId(), castgfx));
							npc.receiveDamage(_master, 160);
						}
						break;
					case 5002:// 死騎娃娃-地獄火
						if (!calculated) {// 沒有預先計算過機率(物理攻擊)
							L1Magic magic2 = new L1Magic(_master, target);
							int magic_dmg2 = magic2.calcMagicDamage(_skillid);
							magic2.commit(magic_dmg2, 0);
							this.broadcastPacketAll(new S_SkillSound(this.getId(), castgfx));// 發送地獄火動畫
						}
						break;
					case 5003:// 獨眼巨人娃娃-巖牢
						if (!calculated) {// 沒有預先計算過機率(物理攻擊)
							L1Magic magic2 = new L1Magic(_master, target);
							int magic_dmg2 = magic2.calcMagicDamage(_skillid);
							magic2.commit(magic_dmg2, 0);
							//this.broadcastPacketAll(new S_SkillSound(this.getId(), castgfx));// 發送巖牢動畫
							if (target instanceof L1PcInstance) {
								L1PcInstance pc = (L1PcInstance) target;
								pc.sendPacketsAll(new S_SkillSound(pc.getId(), castgfx));
								//pc.receiveDamage(_master, magic_dmg2, true, true);
							} else if (target instanceof L1NpcInstance) {
								L1NpcInstance npc = (L1NpcInstance) target;
								npc.broadcastPacketAll(new S_SkillSound(npc.getId(), castgfx));
								//npc.receiveDamage(_master, magic_dmg2);
							}
						}
						break;
					default:
						//測試娃娃有害魔法使用
						skillUse = new L1SkillUse();
						skillUse.handleCommands(null, _skillid, target.getId(), target.getX(), target.getX(), 0,
								L1SkillUse.TYPE_GMBUFF, this);
						break;
					}
				}
			}
		} catch (Exception e) {
			_log.error(e.getLocalizedMessage(), e);
		}
	}
  
  
//  public void startDollSkill(L1Character target, double dmg) {
//    try {
//      if (this._skillid != -1 && _random.nextInt(100) <= this._r) {
//        int skillNum;
//        L1SkillUse skillUse, skillUse2;
//        L1Magic magic = new L1Magic(this._master, target);
//        int magic_dmg = magic.calcMagicDamage(this._skillid);
//        magic.commit(magic_dmg, 0);
//        L1Skills skill = SkillsTable.get().getTemplate(this._skillid);
//        int castgfx = skill.getCastGfx();
//        if (target instanceof L1NpcInstance) {
//          L1NpcInstance npc = (L1NpcInstance)target;
//          if (this._skillid == 33 && npc.getNpcTemplate().is_boss())
//            return; 
//          if (this._skillid == 29 && npc.getNpcTemplate().is_boss())
//            return; 
//        } 
//        target.broadcastPacketX10((ServerBasePacket)new S_SkillSound(target.getId(), castgfx));
//        if (target instanceof L1PcInstance) {
//          L1PcInstance pc = (L1PcInstance)target;
//          pc.sendPackets((ServerBasePacket)new S_SkillSound(pc.getId(), castgfx));
//        } 
//        switch (this._skillid) {
//          case 4:
//          case 6:
//          case 7:
//          case 10:
//          case 15:
//          case 16:
//          case 17:
//          case 18:
//          case 22:
//          case 25:
//          case 28:
//          case 30:
//          case 34:
//          case 38:
//          case 45:
//          case 46:
//          case 50:
//          case 58:
//          case 65:
//          case 74:
//          case 77:
//          case 108:
//          case 132:
//          case 184:
//          case 187:
//          case 192:
//          case 194:
//          case 202:
//          case 203:
//          case 207:
//          case 208:
//          case 213:
//          case 5003:
//          case 5004:
//          case 5005:
//            broadcastPacketAll((ServerBasePacket)new S_DoActionGFX(getId(), 67));
//            break;
//          case 29:
//            switch (target.getMoveSpeed()) {
//              case 0:
//                if (target instanceof L1PcInstance) {
//                  L1PcInstance pc = (L1PcInstance)target;
//                  pc.sendPackets((ServerBasePacket)new S_SkillHaste(pc.getId(), 2, skill.getBuffDuration()));
//                } 
//                target.broadcastPacketAll((ServerBasePacket)new S_SkillHaste(target.getId(), 2, skill.getBuffDuration()));
//                target.setMoveSpeed(2);
//                break;
//              case 1:
//                skillNum = 0;
//                if (target.hasSkillEffect(43)) {
//                  skillNum = 43;
//                } else if (target.hasSkillEffect(54)) {
//                  skillNum = 54;
//                } else if (target.hasSkillEffect(1001)) {
//                  skillNum = 1001;
//                } 
//                if (skillNum != 0) {
//                  target.removeSkillEffect(skillNum);
//                  target.setMoveSpeed(0);
//                } 
//                break;
//            } 
//            break;
//          case 11:
//            skillUse = new L1SkillUse();
//            skillUse.handleCommands(null, this._skillid, target.getId(), target.getX(), target.getX(), 0, 4, 
//                this);
//            break;
//          case 33:
//            skillUse2 = new L1SkillUse();
//            skillUse2.handleCommands(null, this._skillid, target.getId(), target.getX(), target.getX(), 0, 4, 
//                this);
//            break;
//          case 5000:
//            if (target instanceof L1PcInstance) {
//              L1PcInstance pc2 = (L1PcInstance)target;
//              pc2.sendPacketsAll((ServerBasePacket)new S_SkillSound(pc2.getId(), castgfx));
//              pc2.receiveDamage(this._master, 80.0D, true, true);
//              break;
//            } 
//            if (target instanceof L1NpcInstance) {
//              L1NpcInstance npc2 = (L1NpcInstance)target;
//              npc2.broadcastPacketAll((ServerBasePacket)new S_SkillSound(npc2.getId(), castgfx));
//              npc2.receiveDamage(this._master, 80);
//            } 
//            break;
//          case 5001:
//            if (target instanceof L1PcInstance) {
//              L1PcInstance pc2 = (L1PcInstance)target;
//              pc2.sendPacketsAll((ServerBasePacket)new S_SkillSound(pc2.getId(), castgfx));
//              pc2.receiveDamage(this._master, 160.0D, true, true);
//              break;
//            } 
//            if (target instanceof L1NpcInstance) {
//              L1NpcInstance npc2 = (L1NpcInstance)target;
//              npc2.broadcastPacketAll((ServerBasePacket)new S_SkillSound(npc2.getId(), castgfx));
//              npc2.receiveDamage(this._master, 160);
//            } 
//            break;
//			case 5002:// 死騎娃娃-地獄火
//				if (!calculated) {// 沒有預先計算過機率(物理攻擊)
//					L1Magic magic2 = new L1Magic(_master, target);
//					int magic_dmg2 = magic2.calcMagicDamage(_skillid);
//					magic2.commit(magic_dmg2, 0);
//					this.broadcastPacketAll(new S_SkillSound(this.getId(), castgfx));// 發送地獄火動畫
//				}
//				break;
//			case 5003:// 獨眼巨人娃娃-巖牢
//				if (!calculated) {// 沒有預先計算過機率(物理攻擊)
//					L1Magic magic2 = new L1Magic(_master, target);
//					int magic_dmg2 = magic2.calcMagicDamage(_skillid);
//					magic2.commit(magic_dmg2, 0);
//					//this.broadcastPacketAll(new S_SkillSound(this.getId(), castgfx));// 發送巖牢動畫
//					if (target instanceof L1PcInstance) {
//						L1PcInstance pc = (L1PcInstance) target;
//						pc.sendPacketsAll(new S_SkillSound(pc.getId(), castgfx));
//						//pc.receiveDamage(_master, magic_dmg2, true, true);
//					} else if (target instanceof L1NpcInstance) {
//						L1NpcInstance npc = (L1NpcInstance) target;
//						npc.broadcastPacketAll(new S_SkillSound(npc.getId(), castgfx));
//						//npc.receiveDamage(_master, magic_dmg2);
//					}
//				}
//				break;
//			default:
//				//測試娃娃有害魔法使用
//				skillUse = new L1SkillUse();
//				skillUse.handleCommands(null, _skillid, target.getId(), target.getX(), target.getX(), 0,
//						L1SkillUse.TYPE_GMBUFF, this);
//				break;
//        } 
//      } 
//    } catch (Exception e) {
//      _log.error(e.getLocalizedMessage(), e);
//    } 
//  }
  
  public void set_skill(int int1, int int2) {
    this._skillid = int1;
    if (this._skillid != -1) {
      setLevel(this._master.getLevel());
      setInt(this._master.getInt());
    } 
    this._r = int2;
  }
  
  public int get_skillid() {
    return this._skillid;
  }
  
  public int get_skillrandom() {
    return this._r;
  }
  
  public void set_olX(int x) {
    this._olX = x;
  }
  
  public int get_olX() {
    return this._olX;
  }
  
  public void set_olY(int y) {
    this._olY = y;
  }
  
  public int get_olY() {
    return this._olY;
  }
  
  public L1ItemInstance getInstance() {
    return this._item;
  }
  
  public void setInstance(L1ItemInstance item) {
    this._item = item;
  }
  
  /*
26	0	0	通畅气脉术
42	0	0	体魄强健术
149	0	0	风之神射
48	0	0	祝福魔法武器
151	0	0	大地防护
148	0	0	火焰武器
168	0	0	钢铁防护
163	0	0	烈焰武器
166	0	0	暴风神射
79	0	0	灵魂升华
68	0	0	圣结界
   */
  
	// 輔助類型娃娃
	public void startDollSkill() {
		if (this.get_isSupperDoll()) {
			if (!_type1.getPowerList().isEmpty() ) {
				// int i = 0;
				if (_master instanceof L1PcInstance) {
					final L1PcInstance masterpc = (L1PcInstance) _master;
					for (L1DollExecutor p : _type1.getPowerList()) {
						if (p.is_reset()) {
							p.setDoll(masterpc);
						}			
					}
				}
			}
		}else {
		if (!_type.getPowerList().isEmpty() ) {
			// int i = 0;
			if (_master instanceof L1PcInstance) {
				final L1PcInstance masterpc = (L1PcInstance) _master;
				for (L1DollExecutor p : _type.getPowerList()) {
					if (p.is_reset()) {
						p.setDoll(masterpc);
					}			
				}
			}
		}
		}
	}
	
	
//	public boolean noTarget() {
//		if (this._master == null || this._master.isInvisble() || this._master.isDead()) {
//			deleteDoll();
//			return true;
//		}
//		if (this._master.getMapId() != getMapId())
//			return true;
//		if (this._master instanceof L1PcInstance) {
//			L1PcInstance pc = (L1PcInstance) this._master;
//			_skill_id = this._type.getPowerList();
//			if (this._skill_id != null ) {
//				int i = 0;
//				while (i < this._skill_id.length) {
//					int _skillid = this._skill_id[i];
//					if (!pc.hasSkillEffect(_skillid) ) {
//
//						L1Skills _skill = SkillsTable.get().getTemplate(_skillid);
//						L1SkillUse skillUse = new L1SkillUse();
//						skillUse.handleCommands(pc, _skillid, pc.getId(), pc.getX(), pc.getX(),
//								_skill.getBuffDuration(), 4);
//						setSleepTime(calcSleepTime(getAtkspeed(), 1));
//						break;
//					}
//					i++;
//				}
//			}
//			if (pc.getCurrentHp() != pc.getMaxHp()
//					&& pc.getCurrentHp() < pc.getMaxHp() * pc.getHierarch() / 10) {
//				int chance = _random.nextInt(75) + 1;
//				pc.sendPacketsX8((ServerBasePacket) new S_SkillSound(pc.getId(), 830));
//				pc.setCurrentHp(pc.getCurrentHp() + getLawful() + chance);
//				setSleepTime(calcSleepTime(getAtkspeed(), 1));
//			}
//		}
//		if (getLocation().getTileLineDistance((Point) this._master.getLocation()) > 1 && this._npcMove != null) {
//			this._npcMove.setDirectionMove(this._npcMove.moveDirection(this._master.getX(), this._master.getY()));
//			setSleepTime(calcSleepTime(getPassispeed(), 1));
//		}
//		return false;
//	}
//  
	
  public void show_action(int i) {
	if (this.get_isSupperDoll()) {
	    if (!this._type1.getPowerList().isEmpty()) {
	        if (i == 1 && this._master instanceof L1PcInstance) {
	          L1PcInstance pc = (L1PcInstance)this._master;
	          pc.sendPacketsAll((ServerBasePacket)new S_SkillSound(this._master.getId(), 6319));
	        } 
	        if (i == 2 && this._master instanceof L1PcInstance) {
	          L1PcInstance pc = (L1PcInstance)this._master;
	          pc.sendPacketsAll((ServerBasePacket)new S_SkillSound(this._master.getId(), 6320));
	        } 
	        if (i == 3 && this._master instanceof L1PcInstance) {
	          L1PcInstance pc = (L1PcInstance)this._master;
	          pc.sendPacketsAll((ServerBasePacket)new S_SkillSound(this._master.getId(), 6321));
	        } 
	      } 
	}else {
    if (!this._type.getPowerList().isEmpty()) {
      if (i == 1 && this._master instanceof L1PcInstance) {
        L1PcInstance pc = (L1PcInstance)this._master;
        pc.sendPacketsAll((ServerBasePacket)new S_SkillSound(this._master.getId(), 6319));
      } 
      if (i == 2 && this._master instanceof L1PcInstance) {
        L1PcInstance pc = (L1PcInstance)this._master;
        pc.sendPacketsAll((ServerBasePacket)new S_SkillSound(this._master.getId(), 6320));
      } 
      if (i == 3 && this._master instanceof L1PcInstance) {
        L1PcInstance pc = (L1PcInstance)this._master;
        pc.sendPacketsAll((ServerBasePacket)new S_SkillSound(this._master.getId(), 6321));
      } 
    } 
	}
  }
  
  public boolean is_power_doll() {
    return this._power_doll;
  }
}
